package com.xyht.sca_s.student_manage_system.modules.employInfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.employInfo.entity.SmsEmployInfo;
import com.xyht.sca_s.student_manage_system.modules.employInfo.entity.req.SmsEmployInfoReq;
import com.xyht.sca_s.student_manage_system.modules.employInfo.entity.resp.SmsEmployInfoResp;
import com.xyht.sca_s.student_manage_system.modules.employInfo.mapper.SmsEmployInfoMapper;
import com.xyht.sca_s.student_manage_system.modules.employInfo.service.SmsEmployInfoService;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.entity.resp.SmsSearchUserResp;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.employInfo.constant.IsDisplayFlagConstant.IS_DISPLAY_FLAG_TRUE;

/**
 * <p>
 * 招聘信息表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-09-11
 */
@Service
public class SmsEmployInfoServiceImpl extends ServiceImpl<SmsEmployInfoMapper, SmsEmployInfo> implements SmsEmployInfoService {
    @Autowired
    private SmsEmployInfoMapper smsEmployInfoMapper;

    @Autowired
    private CacheUtil cacheUtil;

    @Override
    public ResponseResult addEmployInfo(SmsEmployInfoReq smsEmployInfoReq, String user_id) {
        if (isNullOrEmpty(smsEmployInfoReq.getEmployTitle(), smsEmployInfoReq.getEmployAuthor(), smsEmployInfoReq.getIsDisplay())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsEmployInfo addEmployInfo = new SmsEmployInfo();
        BeanUtils.copyProperties(smsEmployInfoReq, addEmployInfo);
        addEmployInfo.setUserId(user_id);
        addEmployInfo.setSendTime(new Date());

        smsEmployInfoMapper.insert(addEmployInfo);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getEmployInfoBackList(String search_str, Integer pageNum, Integer pageSize) {
        QueryWrapper<SmsEmployInfo> queryWrapper = new QueryWrapper<>();
        if (!isNullOrEmpty(search_str)) {
            queryWrapper.lambda().and(wrapper -> {
                wrapper.like(SmsEmployInfo::getEmployTitle, search_str)
                        .or()
                        .like(SmsEmployInfo::getEmployAuthor, search_str);
            });
        }
        queryWrapper.lambda().orderByDesc(SmsEmployInfo::getCreateTime);

        Page<SmsEmployInfo> page = new Page<>(pageNum, pageSize);
        smsEmployInfoMapper.selectPage(page, queryWrapper);
        List<SmsEmployInfoResp> employInfoRespList = page.getRecords()
                .stream().map(employInfo -> {
                    SmsEmployInfoResp smsEmployInfoResp = new SmsEmployInfoResp();
                    BeanUtils.copyProperties(employInfo, smsEmployInfoResp);

                    SmsUser smsUser = cacheUtil.getUserInfo(employInfo.getUserId());
                    if (smsUser != null) {
                        SmsSearchUserResp smsSearchUserResp = new SmsSearchUserResp();
                        BeanUtils.copyProperties(smsUser, smsSearchUserResp);
                        smsEmployInfoResp.setUserInfo(smsSearchUserResp);
                    }

                    return smsEmployInfoResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(employInfoRespList,total);
    }

    @Override
    public ResponseResult getEmployInfoWebList(String search_str, Integer pageNum, Integer pageSize) {
        QueryWrapper<SmsEmployInfo> queryWrapper = new QueryWrapper<>();
        if (!isNullOrEmpty(search_str)) {
            queryWrapper.lambda().and(wrapper -> {
                wrapper.like(SmsEmployInfo::getEmployTitle, search_str)
                        .or()
                        .like(SmsEmployInfo::getEmployAuthor, search_str);
            });
        }
        queryWrapper.lambda().eq(SmsEmployInfo::getIsDisplay,IS_DISPLAY_FLAG_TRUE)
                .orderByDesc(SmsEmployInfo::getCreateTime);

        Page<SmsEmployInfo> page = new Page<>(pageNum, pageSize);
        smsEmployInfoMapper.selectPage(page, queryWrapper);
        List<SmsEmployInfoResp> employInfoRespList = page.getRecords()
                .stream().map(employInfo -> {
                    SmsEmployInfoResp smsEmployInfoResp = new SmsEmployInfoResp();
                    BeanUtils.copyProperties(employInfo, smsEmployInfoResp);

                    SmsUser smsUser = cacheUtil.getUserInfo(employInfo.getUserId());
                    if (smsUser != null) {
                        SmsSearchUserResp smsSearchUserResp = new SmsSearchUserResp();
                        BeanUtils.copyProperties(smsUser, smsSearchUserResp);
                        smsEmployInfoResp.setUserInfo(smsSearchUserResp);
                    }

                    return smsEmployInfoResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(employInfoRespList,total);
    }

    @Override
    public ResponseResult getEmployInfoDetail(String employ_id) {
        if (isNullOrEmpty(employ_id)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsEmployInfo smsEmployInfo = smsEmployInfoMapper.selectById(employ_id);
        if (isNullOrEmpty(smsEmployInfo)){
            return CommonResult.failed(CommonCodeEnum.EMPLOY_NOT_EXIST);
        }

        SmsEmployInfoResp smsEmployInfoResp = new SmsEmployInfoResp();
        BeanUtils.copyProperties(smsEmployInfo, smsEmployInfoResp);

        SmsUser smsUser = cacheUtil.getUserInfo(smsEmployInfo.getUserId());
        if (smsUser != null) {
            SmsSearchUserResp smsSearchUserResp = new SmsSearchUserResp();
            BeanUtils.copyProperties(smsUser, smsSearchUserResp);
            smsEmployInfoResp.setUserInfo(smsSearchUserResp);
        }
        return CommonResult.success(smsEmployInfoResp);
    }

    @Override
    public ResponseResult updateEmployInfo(SmsEmployInfoReq smsEmployInfoReq, String user_id) {
        if (isNullOrEmpty(smsEmployInfoReq.getEmployTitle(),
                smsEmployInfoReq.getEmployAuthor(),
                smsEmployInfoReq.getIsDisplay(),
                smsEmployInfoReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsEmployInfo smsEmployInfo = smsEmployInfoMapper.selectById( smsEmployInfoReq.getId());
        if (isNullOrEmpty(smsEmployInfo)){
            return CommonResult.failed(CommonCodeEnum.EMPLOY_NOT_EXIST);
        }

        SmsEmployInfo updateEmployInfo = new SmsEmployInfo();
        BeanUtils.copyProperties(smsEmployInfoReq, updateEmployInfo);
        updateEmployInfo.setUserId(user_id);
        updateEmployInfo.setSendTime(new Date());

        smsEmployInfoMapper.updateById(updateEmployInfo);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deleteEmployInfo(SmsEmployInfoReq smsEmployInfoReq, String user_id) {
        if (isNullOrEmpty(smsEmployInfoReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        smsEmployInfoMapper.deleteById(smsEmployInfoReq.getId());
        return CommonResult.success();
    }
}
